package com.eastfair.home.service.impl;

//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.annotation.annotation.cache.Cache;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.home.dao.BoothAcceptanceDetailMapper;
import com.eastfair.home.dto.BoothAcceptanceDetailDTO;
import com.eastfair.home.entity.BoothAcceptanceDetail;
import com.eastfair.home.service.BoothAcceptanceDetailService;
import com.eastfair.home.vo.BoothAcceptanceDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 展台验收记录明细
 * </p>
 *
 * @author linan
 * @date 2022-11-29
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class BoothAcceptanceDetailServiceImpl extends SuperServiceImpl<BoothAcceptanceDetailMapper, BoothAcceptanceDetail> implements BoothAcceptanceDetailService {


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<BoothAcceptanceDetail> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }



    @Override
    protected R<Boolean> handlerSave(BoothAcceptanceDetail model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    /**
     * 通用查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    public List<BoothAcceptanceDetail> list(BoothAcceptanceDetailDTO modelDTO, List<Long> ids) {
        return ((BoothAcceptanceDetailService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
            magic = @Magic(
                    key = "{" +
                            "{'isEnabled', #args[0]?.isEnabled}," +
                            "{'updatedName', #args[0]?.updatedName}," +
                            "{'acceptanceRecordId', #args[0]?.acceptanceRecordId}," +
                            "{'supervisionItemManagementId', #args[0]?.supervisionItemManagementId}," +
                            "{'textBoxContent', #args[0]?.textBoxContent}," +
                            "{'optionOneCheckStatus', #args[0]?.optionOneCheckStatus}," +
                            "{'optionOneReason', #args[0]?.optionOneReason}," +
                            "{'optionTwoCheckStatus', #args[0]?.optionTwoCheckStatus}," +
                            "{'optionTwoReason', #args[0]?.optionTwoReason}," +
                            "{'optionThreeCheckStatus', #args[0]?.optionThreeCheckStatus}," +
                            "{'optionThreeReason', #args[0]?.optionThreeReason}," +
                            "{'optionFourCheckStatus', #args[0]?.optionFourCheckStatus}," +
                            "{'optionFourReason', #args[0]?.optionFourReason}," +
                            "{'optionFiveCheckStatus', #args[0]?.optionFiveCheckStatus}," +
                            "{'optionFiveReason', #args[0]?.optionFiveReason}," +
                            "{'id', #args[1]}" +
                            "}"))
    public List<BoothAcceptanceDetail> listOfCache(BoothAcceptanceDetailDTO modelDTO, List<Long> ids) {
        //查询
        QueryWrapper<BoothAcceptanceDetail> queryWrapper = new QueryWrapper<>();
        if (modelDTO != null) {
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getUpdatedName()), BoothAcceptanceDetail::getUpdatedName, modelDTO.getUpdatedName());
            queryWrapper.lambda().eq(modelDTO.getAcceptanceRecordId() != null, BoothAcceptanceDetail::getAcceptanceRecordId, modelDTO.getAcceptanceRecordId());
            queryWrapper.lambda().eq(modelDTO.getSupervisionItemManagementId() != null, BoothAcceptanceDetail::getSupervisionItemManagementId, modelDTO.getSupervisionItemManagementId());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getTextBoxContent()), BoothAcceptanceDetail::getTextBoxContent, modelDTO.getTextBoxContent());
            queryWrapper.lambda().eq(modelDTO.getOptionOneCheckStatus() != null, BoothAcceptanceDetail::getOptionOneCheckStatus, modelDTO.getOptionOneCheckStatus());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getOptionOneReason()), BoothAcceptanceDetail::getOptionOneReason, modelDTO.getOptionOneReason());
            queryWrapper.lambda().eq(modelDTO.getOptionTwoCheckStatus() != null, BoothAcceptanceDetail::getOptionTwoCheckStatus, modelDTO.getOptionTwoCheckStatus());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getOptionTwoReason()), BoothAcceptanceDetail::getOptionTwoReason, modelDTO.getOptionTwoReason());
            queryWrapper.lambda().eq(modelDTO.getOptionThreeCheckStatus() != null, BoothAcceptanceDetail::getOptionThreeCheckStatus, modelDTO.getOptionThreeCheckStatus());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getOptionThreeReason()), BoothAcceptanceDetail::getOptionThreeReason, modelDTO.getOptionThreeReason());
            queryWrapper.lambda().eq(modelDTO.getOptionFourCheckStatus() != null, BoothAcceptanceDetail::getOptionFourCheckStatus, modelDTO.getOptionFourCheckStatus());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getOptionFourReason()), BoothAcceptanceDetail::getOptionFourReason, modelDTO.getOptionFourReason());
            queryWrapper.lambda().eq(modelDTO.getOptionFiveCheckStatus() != null, BoothAcceptanceDetail::getOptionFiveCheckStatus, modelDTO.getOptionFiveCheckStatus());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getOptionFiveReason()), BoothAcceptanceDetail::getOptionFiveReason, modelDTO.getOptionFiveReason());
        }
        if (ids != null && !ids.isEmpty()) {
            queryWrapper.lambda().in(BoothAcceptanceDetail::getId, ids);
        }
        queryWrapper.lambda().eq(BoothAcceptanceDetail::getIsDeleted, BusinessConstant.DELETE_NO);
        List<BoothAcceptanceDetail> modelList = list(queryWrapper);
        return modelList;
    }

    @Override
    public List<BoothAcceptanceDetailVO> getByAcceptanceRecordId(Long acceptanceRecordId) {
        return baseMapper.getByAcceptanceRecordId(acceptanceRecordId);
    }

    @Override
    public void removeByRecordId(Long acceptanceRecordId) {
        update(new UpdateWrapper<BoothAcceptanceDetail>().lambda().eq(BoothAcceptanceDetail::getIsDeleted,BusinessConstant.NO)
                .eq(BoothAcceptanceDetail::getAcceptanceRecordId,acceptanceRecordId).set(BoothAcceptanceDetail::getIsDeleted,BusinessConstant.YES));
    }


    /**
     * do转vo
     *
     * @param model model
     * @return modelVo
     */
    @Override
    public BoothAcceptanceDetailVO doToVo(BoothAcceptanceDetail model) {
        BoothAcceptanceDetailVO modelVo = new BoothAcceptanceDetailVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }

    /**
     * do转vo
     *
     * @param modelList modelList
     * @return list
     */
    @Override
    public List<BoothAcceptanceDetailVO> doToVos(List<BoothAcceptanceDetail> modelList) {
        if (modelList == null || modelList.isEmpty()) {
            return null;
        }
        List<BoothAcceptanceDetailVO> result = new ArrayList<>();
        for (BoothAcceptanceDetail model : modelList) {
            result.add(doToVo(model));
        }
        return result;
    }
}
